package student.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import student.aistar.model.bo.Result;
import student.aistar.model.dto.AddressDTO;
import student.aistar.model.entity.Address;
import student.aistar.model.entity.User;
import student.aistar.service.IUserService;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/login")
    public String Login(User user){

        userService.Login(user);

        return "mi/login/login";
    }

    @GetMapping("/register")
    public String Register(){
        return "mi/register/register";
    }


    @PostMapping("/loginIndex")
    @ResponseBody
    public Result loginIndex(User user, HttpSession session){
        //1. 调用根据用户查找的方法
        User u = userService.loginIndex(user);
        if(u==null)
            return new Result("404","用户名不存");

        if(u.getUserPassword().equals(user.getUserPassword())){
            session.setAttribute("user", u);
            return new Result("200","登录成功");
        }

        return new Result("500","密码错误");
    }


//    @GetMapping("/findAddressById")
//    public String findAddressById(HttpSession session){
//        User user = (User) session.getAttribute("user");
//        AddressDTO addressDTO = userService.findAddressById(user.getUserId());
//
//
//        session.setAttribute("addressList", addressDTO.getAddressList());
//        session.setAttribute("addressDetailList", addressDTO.getAddressDetailList());
//
//        return
//    }


//    @GetMapping

    @GetMapping("/exit")
    public String exit(HttpSession session){
        session.invalidate();
        return "redirect:index";
    }
}

